package com.bayi.storage.network;

import com.bayi.storage.base.BaseApplication;
import com.bayi.storage.network.entity.LoggerInterceptor;
import com.bayi.storage.network.entity.NullOnEmptyConverterFactory;

import java.util.concurrent.TimeUnit;

import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.fastjson.FastJsonConverterFactory;

/**
 * @author xp
 * 网络请求类
 */
public class RetrofitHelper {
    //设置超时时间
    public static final long DEFAULT_TIMEOUT = 10_000L;
    public static final int CODE = 200;
    private static final String ENC_KEY = "A0jh2aAt";
    private static final String SOURCE = "1002";
    private Retrofit retrofit;
    private OkHttpClient client;


    private static class SingletonHolder {
        private static final RetrofitHelper INSTANCE = new RetrofitHelper();
    }

    //私有化构造方法
    private RetrofitHelper() {
        client = new OkHttpClient.Builder()
                .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS) //读取超时
                .writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS) //写超时
                .addInterceptor(chain -> {
                    Request original = chain.request();
                    HttpUrl originalHttpUrl = original.url();
                    HttpUrl url = originalHttpUrl
                            .newBuilder()
                            .build();
//                    String token = SharedPreferenceUtil.getSharedPreferenceUtil(BaseApplication.getApp()).getInfoFromShared(Constant.TOKEN,"");
                    Request request = original
                            .newBuilder()
                            .url(url)
                            .header("Content-Type", ContentType)
                            .addHeader("Connection", "keep-alive")
//                            .addHeader("ACCESS-TOKEN", token)
//                                .addHeader("Accept", "*/*")
//                                .addHeader("Accept-Encoding", "gzip,defalte,br")
                            .build();
                    Response response = chain.proceed(request);
                    return response;
                })
                //添加日志打印拦截器
                .addInterceptor(new LoggerInterceptor("NETWORKURL", true))
                .build();
        retrofit = new Retrofit.Builder()
                .baseUrl(BaseApplication.getApp().getStorageCabinetModel().getHttp())
                .client(client)
                //自定义一个ConverterFactory
                .addConverterFactory(new NullOnEmptyConverterFactory())
                //添加Gson解析
                .addConverterFactory(FastJsonConverterFactory.create())
                //添加rxjava
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
    }

    private static String ContentType = "application/json";

    public static RetrofitHelper getInstance(String contentType) {
        ContentType = contentType;
        return SingletonHolder.INSTANCE;
    }

    public static RetrofitHelper getInstance() {
        ContentType = "application/json";
        return SingletonHolder.INSTANCE;
    }

    public <T> T createService(Class<T> clazz) {
        return retrofit.create(clazz);
    }

}
